package com.taxsearch.util.payuntil;

import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;





public class HttpClientUtil {
	private static final Logger logger = Logger.getLogger(HttpClientUtil.class);

	private HttpClient httpClient;
	private String namespace;
	private String methodName;
	private String wsdlLocation;
	private Map<String, String> patameterMap;
	private PageData pd;
	private String xml;



	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public String getWsdlLocation() {
		return wsdlLocation;
	}

	public void setWsdlLocation(String wsdlLocation) {
		this.wsdlLocation = wsdlLocation;
	}

	public Map<String, String> getPatameterMap() {
		return patameterMap;
	}

	public void setPatameterMap(Map<String, String> patameterMap) {
		this.patameterMap = patameterMap;
	}

	public PageData getPd() {
		return pd;
	}

	public void setPd(PageData pd) {
		this.pd = pd;
	}


	public HttpClientUtil() {

	}

	public HttpClientUtil(String namespace, String methodName,
			String wsdlLocation, HttpClient httpClient,
			Map<String, String> patameterMap) {
		this.namespace = namespace;
		this.methodName = methodName;
		this.wsdlLocation = wsdlLocation;
		this.httpClient = httpClient;
		this.patameterMap = patameterMap;
	}

	public HttpClientUtil(String wsdlLocation,PageData pd,HttpClient httpClient){
		this.wsdlLocation=wsdlLocation;
		this.pd=pd;
		this.httpClient=httpClient;
	}
	
	public HttpClientUtil(String wsdlLocation,String xml,HttpClient httpClient){
		this.wsdlLocation=wsdlLocation;
		this.xml=xml;
		this.httpClient=httpClient;
	}

	public Hc invokePost(){
		Hc hc=new Hc();
		hc.setStatus(0);
		PostMethod postMethod = new PostMethod(wsdlLocation);
		
		try{
			
		 Part[] parts=new Part[pd.size()];
		 Set<String> s=pd.keySet();
		 int i=0;
		 for (String str : s) {
		      StringPart sp=new StringPart(str, (String)pd.get(str),"UTF-8");
		      parts[i]= sp;
		      i=i+1;
		}
         MultipartRequestEntity mre=new MultipartRequestEntity(parts,postMethod.getParams());
		 postMethod.setRequestEntity(mre);
		 postMethod.getParams().setParameter(HttpMethodParams.HTTP_ELEMENT_CHARSET,"UTF-8");
		 postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
		 int status = httpClient.executeMethod(postMethod);
		 hc.setStatus(status);
		 String respon = "";            
         respon = postMethod.getResponseBodyAsString();
         hc.setRespon(respon);
//		 if (status != HttpStatus.SC_OK) {
//
//         } else if (status == HttpStatus.SC_OK) {
//
//         }

		}catch(Exception e){
			e.printStackTrace();
		}finally {
			postMethod.releaseConnection();
			return hc;
		}
	}
	
	public Hc invokeXml(){
		Hc hc=new Hc();
		hc.setStatus(0);
		PostMethod postMethod = new PostMethod(wsdlLocation);
        
        
		 try {
			 RequestEntity entity = new StringRequestEntity(xml, "text/xml",  "UTF-8");  
		        postMethod.setRequestEntity(entity); 
		        postMethod.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
		        int status = httpClient.executeMethod(postMethod);
		        hc.setStatus(status);
		        String respon = "";            
	             respon = postMethod.getResponseBodyAsString();
	             hc.setRespon(respon);
//				 if (status != HttpStatus.SC_OK) {
//					 
//		         } else if (status == HttpStatus.SC_OK) {
//		             
//		         }

				}catch(Exception e){
					e.printStackTrace();
				}finally {
					postMethod.releaseConnection();
					return hc;
				}
	}
}
